package cn.itcast.service;


import cn.itcast.dao.MemberMapper;
import cn.itcast.domain.Member;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.utils.MD5Utils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {


    @Autowired
    private MemberMapper memberMapper;

    /**
     * 根据手机号来判断是否是会员
     *
     * @param telephone
     * @return
     */
    @Override
    public Member findByTelephone(String telephone) {

        return memberMapper.findByTelephone(telephone);
    }

    /**
     * 注册会员
     *
     * @param member
     */
    @Override
    public void insert(Member member) {
        if (member.getPassword() != null) {
            //MD5Utils工具类将密码加密 :明文=>密文
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberMapper.add(member);
    }

    /**
     * 折线图
     *
     * @param months
     * @return
     */
    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> list = new ArrayList<>();
        for (String m : months) {
            //拼接字符串 每月查询
            m = m + ".31";//格式：2019.04.31
            Integer count = memberMapper.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;
    }


    /**
     * 新增会员
     *
     * @param member
     */
    @Override
    public void insertMember(Member member) {
        memberMapper.insertMember(member);
    }

    /**
     * 数据回显
     *
     * @param id
     * @return
     */
    @Override
    public Member findMemberById(Integer id) {
        return memberMapper.findMemberById(id);
    }

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findMemberByPage(QueryPageBean queryPageBean) {
        //健壮性判断
        Integer currentPage = queryPageBean.getCurrentPage();
        if (currentPage == null) {
            currentPage = 1;
        }
        Integer pageSize = queryPageBean.getPageSize();
        if (pageSize == null) {
            pageSize = 10;
        }

        PageHelper.startPage(currentPage, pageSize);
        Page<Member> page = (Page<Member>) memberMapper.findMemberByPage(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void deleteMember(Integer id) {

        memberMapper.deleteMember(id);
    }

    /**
     * 修改
     *
     * @param member
     */
    @Override
    public void updateMember(Member member) {

        memberMapper.updateMember(member);
    }


    /**
     * 男女比例图 饼形图
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> findGenderCount() {

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<String, Object> map3 = new HashMap<>();
        //查询未知性别
        Integer nullCount = memberMapper.findByNull();
        Integer sex1Count = memberMapper.findBySex1();
        Integer sex2Count = memberMapper.findBySex2();
        map1.put("name", "未知性别");
        map1.put("value", nullCount);
        map2.put("name", "男");
        map2.put("value", sex1Count);
        map3.put("name", "女");
        map3.put("value", sex2Count);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        return list;
    }

    @Override
    public List<Map<String, Object>> findAgeCount() {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<String, Object> map3 = new HashMap<>();
        Map<String, Object> map4 = new HashMap<>();
        //查询不同年龄段
        /* 例如0-18、18-30、30-45、45以上）*/
        Integer count1 = memberMapper.findByAge1();
        Integer count2 = memberMapper.findByAge2();
        Integer count3 = memberMapper.findByAge3();
        Integer count4 = memberMapper.findByAge4();
        map1.put("name", "0-18");
        map1.put("value", count1);
        map2.put("name", "18-30");
        map2.put("value", count2);
        map3.put("name", "30-45");
        map3.put("value", count3);
        map4.put("name", "45以上");
        map4.put("value", count4);
        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        return list;
    }

}


